package com.zhongxi.guoyuan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.GiftAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.gift.GiftRuleResult;
import com.zhongxi.guoyuan.entity.gift.NoviceItemData;
import com.zhongxi.guoyuan.entity.gift.NoviceItemResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.HtmlActivity;
import com.zhongxi.guoyuan.view.user.gift.GiftDetailActivity;
import com.zhongxi.guoyuan.widget.sliding.NoScrollListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;

public class NewPersonGiftFragment extends BaseFragment implements View.OnClickListener {

    private NoScrollListView lvList;
    private String type;

    private List<NoviceItemData> data = new ArrayList<NoviceItemData>();
    private GiftAdapter adapter;
    private Map<String, String> map = new HashMap<String, String>();
    private GiftRuleResult result;

    private ImageView ivLogo;
    private NoviceItemResult noviceItemResult;
    public static final int requestGift = 10;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_new_person_gift, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        super.initView();
        initTitle();
        tvTitle.setText("快乐分享");
        lvList = (NoScrollListView) getView().findViewById(R.id.lvList);
        ivLogo = (ImageView) getView().findViewById(R.id.ivLogo);

    }

    @Override
    protected void initData() {
        super.initData();
        type = "rule";
        getServer(Url.RULE_TPL, null, "", false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if ("rule".equals(type)) {
                result = gson.fromJson(response, GiftRuleResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if (null != result.getData() && !"".equals(result.getData())) {
                    if (null != result.getData().getImages() && !"".equals(result.getData().getImages())) {

                        Glide.with(context)
                                .load(Url.IMG_URL + result.getData().getImages())
                                .placeholder(R.mipmap.iv_gift)
                                .into(ivLogo);
                    }
                }
                getProject();
            } else if ("project".equals(type)) {
                noviceItemResult = gson.fromJson(response, NoviceItemResult.class);
                MyApplication.getInstance().setSpValue(noviceItemResult.getSafeobj());
                data.clear();
                if (null != noviceItemResult.getActivity() && noviceItemResult.getActivity().size() > 0) {
                    data.addAll(noviceItemResult.getActivity());
                }

                adapter = new GiftAdapter(context, data);

                JSONObject jsonObject = new JSONObject(response);
                JSONObject item = jsonObject.optJSONObject("item");
                JSONObject market = jsonObject.optJSONObject("market");
                JSONObject goods = jsonObject.optJSONObject("goods");
                if (goods != null) {
                    adapter.setGoods(goods);
                }
                if (item != null) {
                    adapter.setItem(item);
                }
                if (market != null) {
                    adapter.setMarket(market);
                }
                lvList.setAdapter(adapter);
                adapter.notifyDataSetChanged();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse res = gson.fromJson(response, BaseResponse.class);
            if (null != res.getSafeobj() && !"".equals(res.getSafeobj())) {
                MyApplication.getInstance().setSpValue(res.getSafeobj());
            }

            if (res.getCode() == 1000) {
                showShortToast("参数错误 《各接口相关》");
            }
            showShortToast("条件不满足，请查看领取时间或美币是否充足");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 活动礼包项目
     */
    private void getProject() {
        type = "project";
        getServer(Url.GIFT_LIST, null, "", false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        ivLogo.setOnClickListener(this);

        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != data.get(position).getId() && !"".equals(data.get(position).getId())) {
                    Intent intent = new Intent(context, GiftDetailActivity.class);
                    intent.putExtra("aid", data.get(position).getId());
                    startActivityForResult(intent, requestGift);
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                getActivity().finish();
                break;
            case R.id.ivLogo:
                if (!TextUtils.isEmpty(result.getData().getDescription())) {
                    Intent intent = new Intent(context, HtmlActivity.class);
                    intent.putExtra("content", result.getData().getDescription());
                    intent.putExtra("type", "activityRule");
                    startActivity(intent);
                }
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == requestGift && resultCode == GiftDetailActivity.resultGift) {
            getProject();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
